﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.Net;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using FluorineFx;

namespace test46
{
    class Monitor
    {
        private int PORT = 8888;
        private const int MAXPKSIZE = 4096;
        private TcpListener tcpLs = null;
        private List<Client> clients = new List<Client>();
        private MapInfo2 aMapInfo = new MapInfo2();
        private int LastUserId;

        public void Close()
        {
            if (tcpLs != null)
            {
                tcpLs.Stop();
            }
            if (clients.Count != 0)
            {
                foreach (Client c in clients)
                {
                    c.sock.Shutdown(SocketShutdown.Both);
                    c.thread.Abort();
                }
                clients.Clear();
                clients = null;
            }
        }

        private void ThreadFunc(object client)
        {
            Client ct = client as Client;
            Socket sk = ct.sock;
            byte[] cmdBuff = new byte[MAXPKSIZE];
            FluorineFx.AMF3.ByteArray ba = new FluorineFx.AMF3.ByteArray();
            while (true)
            {
                try
                {
                    if (sk.Available<=0)
                        continue;
                    cmdBuff.Initialize();
                    int N = sk.Receive(cmdBuff);
                   // sk.
                    Console.WriteLine(DateTime.Now.ToString() + ":接收数据成功,长度:" + N);
                    if (N <= 0)
                        continue;
                    //System.IO.MemoryStream ms = new System.IO.MemoryStream(cmdBuff, 0, N);
                    //FluorineFx.AMF3.ByteArray ba = new FluorineFx.AMF3.ByteArray(ms);
                    ba.Position = 0;
                    ba.WriteBytes(cmdBuff, 0, N);
                    ba.Position = 0;
                    MessageBase aMessage = (MessageBase)ba.ReadObject();

                    if (aMessage.MessageType == 1)
                    {
                        //aMapInfo.TimeTicks = DateTime.Now.Ticks;
                        FluorineFx.AMF3.ByteArray result = new FluorineFx.AMF3.ByteArray();
                        MessageBase ss = aMapInfo;
                        result.WriteObject(ss);
                        byte[] buf = result.GetBuffer();
                        sk.Send(buf, (int)(result.Length), SocketFlags.None);
                    }
                    else if (aMessage.MessageType == 3)
                    {
                        PathInfo aPathInfo = (PathInfo)aMessage;
                        aPathInfo.UserId = ct.UserId;
                        ct.CurPath = aPathInfo;
                        ct.StartTime = DateTime.Now;
                        FluorineFx.AMF3.ByteArray result = new FluorineFx.AMF3.ByteArray();
                        result.WriteObject(aPathInfo);
                        byte[] buf = result.GetBuffer();

                        foreach (Client cl in clients)
                        {
                            if(cl.sock != sk)
                            {
                                cl.sock.Send(buf, (int)(result.Length), SocketFlags.None);
                            }
                            
                        }
                    }
                    else if (aMessage.MessageType == 4)
                    {
                        ActorsPosition aActorsPosition = GetActorsPosition(ct);
                        FluorineFx.AMF3.ByteArray result = new FluorineFx.AMF3.ByteArray();
                        result.WriteObject(aActorsPosition);
                        byte[] buf = result.GetBuffer();
                        sk.Send(buf, (int)(result.Length), SocketFlags.None);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("网络终止:" + e.Message);
                    ct.sock.Shutdown(SocketShutdown.Both);
                    clients.Remove(ct);
                    RemoveUser aRemoveUser = new RemoveUser();
                    aRemoveUser.UserId = ct.UserId;
                    FluorineFx.AMF3.ByteArray result = new FluorineFx.AMF3.ByteArray();
                    result.WriteObject(aRemoveUser);
                    byte[] buf = result.GetBuffer();
                    foreach (Client cl in clients)
                    {
                            cl.sock.Send(buf, (int)(result.Length), SocketFlags.None);
                    }
                    Thread.CurrentThread.Abort();
                    ct = null;
                }
            }
        }
        public void StartUp()
        {
            LastUserId = 1;
            aMapInfo.ColCount = 100;
            aMapInfo.RowCount = 100;
            aMapInfo.CellWidth = 14;
            aMapInfo.CellHeight = 14;
            //aMapInfo.CellValues = "1000000001110000000011000101000010111000000001111101001010000000000101100010100110100110000010001010000011010110001000110000000111011010000100011110010110100001000000100011001101101000000000000111100000001110100001110100100000100000100001000001000000111010001011001100000000000000101110000101101011101001100100011010000001001011000000000101100100010100001001001111000110100110000010010000000010000001000111101100011000100101100000100111000001001000010000110000000000000010001000001000001101000001110000110000010001100010011110101010000001000001000011101000010100000001011110000110000001000000100110101000010011000110010010010010010001000000001010100010000000001010110011000101011100000000000110000000000000100000010011000000110010001000000010010000000110000110000110010000100000101010100110011110000000000001100000100101010011100010100000010011000110010001001111001010010011000111001000001001100011000000000000011001000000010010000001000010000001001101000000111100111000001011010101000000100000101000010000000001001001010001011110001010110000011000010100110011000101010000001000000000100100000110000000000000011110000010100000001010100001001100000001010011101000000011101001000000001000010100000000000000001100110100100000000000100000100000100010100000100000000010000101000010001100000000110000000100011000010100011000101010101000000100101000111001110010110000000101000000010100100000000000000000111000110000011000000001100011001000100000110000001001110000001010001001100000000101001100010001000011100000100000000001000101101000001000000000100000010001011011001010001000000111101100001001001010000000000000000001000000010000001101000101011000000001001000000110100100000000001101000101000000100000010010001010000000000000000001000001100001100000001100000011010000100100100100000001100101000010110100000010001010001100000100011101111001001101000010010110010000000111010101100011110111000101000000010100001110101001001010000011010011101100010000000100100001000010100010000010110000101000101000100000000001001000010100100000110000001000110100101010010100101011000000011101001100110000010000111001100001000111000000111010000001100110101010001100100100000100010101111000000110000000110000000000101000111100101000001000000100010010000001010101110010001000111001100000001010110001000000110011100000100001000000000100100000000001010000101000000000001100100000010000100000000001100100010101010000100111000000000000000110110101100100010001101110100100101000100000000000000010011001011101000000111010000110000001000000001010000010011000111011010100000000000000000000000000000010010111101011000011110010000000001000100000001000010000000010000001000010111001100000000101000010100100011000010010011011000001110000000110011000010001001100000000000010011001110011000000011010010000000000000111000011100100110110100000001010000000001100000101000110011001100010010100010000000100010000010000001000000010000010010011000000001000001100110001001010000101000000001100000001011001100100001010000100010001100011100100000010010100001010001111000000000100100001000001000010000111010110011100110001000000000000010000000010010000000000001100110010000000011100001010010110001001011001110000011110000101001100101000000100110001000010010000110000011000101100000001000110100001101000000000001010000110001001000100110011010101001000000010100010100010100100000000000010010000000100000100000010100000000011110000100011100010001000001011000111110011111011110100001000100101000000001000000010000110100000000001000001000000010011000011100100010100100001000010011000010000000000000001000010001010000110101000110000000100000011100100101100100000010001010000110000110010011000101010100010010100100110011010001000001000000110000010000000100001010001001000010100101010100100001000000011000000010010010000001001000000000110000000000000100000000010000001100001000001000001000000000010110010010010011010000011000000100000010000001000011100000000100010011000100000001100110000000000000001110000111001110001100000001000000011001000010100000001001101100100110000000101001010111010001100101000001000001100100001100000001001110001000000100000110000000010000000000101101100011001000010000101100000000000010000000011000010100100001000010110001000010000001000000000010000000000101010100001000101001100110000100000000100100010000000110000011100000001000100111000010000000000010001000100001101000000110100010110101001000010100000100100100000000000001001000101100011011000000011001001010010010110010001000001010000000000000000000000100110001110000000001000000110101010001001001111001000101001000111000001110010100000100001010000001000010011011000100110011000011001000000100110001000001000000001000100000000000000100000100011000111100000100001101000010110001010100010011110001001011011100010101000100010000010001010010011111110110100000010000011001000001100100011000000100000000010011100000010101000000000010001010001000110011000000000100000001100010100100000101000000001101100001100010100000100011010100101010000000010011000000001111000000001001000011010011010001010101010000010111000110010000001100100000101000001010010100011010000110000000100000011000010001011000100011101000000010000000000010000100000011100000001010000010011000000110001001101001000011110000001000010110000010001100010101011000001000001000000000100000011000001000010110100101010011000110010100100000001001000010011010001111000101110100010010100010001001001000010100100010110000000100000001101100100100000011000000110101001010110100000010100000000010100000000000000000100100100000000000001001011001000100100011100111000000000001111100000011000010000000000000111010010110101000001001011000000010000000000010000010101100111000010101101110100000000000000101000000101000010000110010000000110110001000001100001000001101111000010000100011011110000101000100000001110011000010000110000000001010000001100101000001010000000010000000100000001000010010100100110111111000010000100001000000000101010111001000011011111000010100000000100010100010000010101000100000101101101101001000100000101110001100000001000000001001000000010110011110010000000100001001000000101000000010000000110101011100011101000110000100100010010100100101001000000001100010000001000010000010110100010010101011000000000000000000101100100000000010110001000000101010001100001000000100100010000001000010010001100101010100000100000010000101000001000110101001010001011000001010000000011100010000100000100000110000111100001000001000010000010001000000001000001011000000100000111100000000010000111000100000000000000011010110100000011000000011000100100000000111010101110001100001000000000000101000100010000010010100000011000010001101000100011101010100010000101010000001100011000000010000000100010000001111000011000100001100110000001110101100000000010000010000100000101001001100000100000100110000000001000111101000000000100000010101100001111000010001110010000000000110000000001101010001000000000100011000100010100101001110000110000100000000110000001000001101001001000100110110000010101011101110000000000000000010000011101100011000000100110000010001001010100000011010001000101010000000000001010101110000100000101010110001011010111000000010000010000100010010000001001101001000101000000101101100001100010001010001001001000000000110001000101000000001000010101110000100011110000001000000010000000111100010001101011101000010011010010000100000000111110000010000010000000101110000011100100011010001000010000001010110110100011100000000000100000010000000100000000000100100001000100110000010010000100010000100000000000100001000001100001000011110010101010000110100010010010000001100100000010110000011011011011010011100010110001000100001011011100000001011010100100100000010000100100000000100000100010001010000100000000101001001000100101001110010111000000001011101000011101000001000101100000010010001010000000000010000000010101110000101011101100100000000011100110101000001111100000001010100101001100111000000010000000000000100011100100111011000100001001100110100100100000000000100101010111000000101000001010100000001000000001010100000101000000000100000000000100000001001100011100000010010000010001010101100110111110110000001000100000011110110100001000000010100010010001111100000000001100000000110010000010000000000010100011000010001010000111110100000010010011100101000000100100000110110000000010010000010100010000111011000011000010100011001100010001110000000000101000011010000100011100001111000000100001101110011100010000110110100010000010101000101011110010000010011100110000000011110000100010110100000101000011010100011000000100101000010000000000001000000001101000011101100010010100101000101000010100001000001100011010010101001100101100110001001010001011010010010001000100000000000000000110010010000001010100000000100000010111000001111111100100001000001010000100001000000010000001000100000100101010000001100000010001001001010000100000011000100000010000011000100010111100000001101000000111000010000000000000001000100011011100000100111000010010111001000100011011000001000100001010001011000001110001101000011000010001001100010000001000000010001001000000000001001000000100001001000100000101101000010000001011011000011101010101101100100101101000001000010001000000000001010000011001000011010011000100001110010000000000010110000000010001001000011000010001111000111100011001000011000001010100101100000010101000011110001110100100100011001100100101001101000010010010010100010010000011000010000010010000000100000001000000101000100001001001001000110011000001110011010001000001001001000011000000000010010010100010100010010001000000001001000100011100000000000110100001000110001100000010000000000010010100011001000111011001001000101000100010001000000010010101000010100101000100100011000101010000010111001000100001000100001000110111000010110100000100000000000110100000001000001100010111000100000010100001101000100100110001110100011001000100000100011100010100110000101010100001110001001110010100100001000000110010001101001000000100000110001010100000000010000000000000010011000100101000100001100110000000000001011111010000010110001011000001001010101000000101000010010010100001";
            aMapInfo.CellValues = "map1.txt";
            aMapInfo.MoveSpeed = 40;
            aMapInfo.StartIndex = 0;
            IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName());
            IPAddress ip = Dns.GetHostAddresses(Dns.GetHostName())[1];
            tcpLs = new TcpListener(ip, PORT);
            tcpLs.Start();
            while (true)
            {
                byte[] packetBuff = new byte[MAXPKSIZE];
                Console.WriteLine("服务器已启动，正在监听...\n");
                Console.WriteLine(string.Format("服务器IP：{0}\t端口号：{1}\n", ip, PORT));

                Socket sk = tcpLs.AcceptSocket();
                Console.WriteLine("已经连接，接收数据...\n");
                Thread cth = new Thread(new ParameterizedThreadStart(ThreadFunc));
                Client cl = new Client(sk, cth);
                cl.UserId = LastUserId;
                LastUserId++;
                clients.Add(cl);
                cth.Start(cl);
            }
        }

        public ActorsPosition GetActorsPosition(Client SelfClinet)
        {
            ActorsPosition aActorsPosition = new ActorsPosition();
            foreach (Client cl in clients)
            {
                if (cl != SelfClinet && cl.CurPath!=null)
                {
                    PathInfo2 aPathInfo2 = new PathInfo2();
                    aPathInfo2.aPathInfo = cl.CurPath;
                    aPathInfo2.EclipseTime = (DateTime.Now - cl.StartTime).TotalSeconds;
                    //aPathInfo2.
                    aActorsPosition.PathInfos.Add(aPathInfo2);
                    //aActorsPosition.EclipseTime = 
                }
            }
            return aActorsPosition;
        }

    }
}
